package ru.hh.school.commands;

import ru.hh.school.User;
import ru.hh.school.UserManager;
import ru.hh.school.logging.Logger;

import static ru.hh.school.Constants.LINE_SEPARATOR;
import static ru.hh.school.util.ResourceManager.message;

public class OnlineCommand extends Command {
    private final UserManager userManager;

    public OnlineCommand(Logger logger, UserManager userManager) {
        super(logger);
        this.userManager = userManager;
    }

    private String constructListOfAllUsers() {
        //TODO make it easier, reuse StringUtils join or smth
        //TODO no need in LineSep after last line
        StringBuilder builder = new StringBuilder();
        boolean first = true;
        for (User user : userManager.getUsers()) {
            if (!first) {
                builder.append(LINE_SEPARATOR);
            }
            builder.append(message("command.online.user", user.getLogin(), user.getRoom()));
            first = false;
        }
        return message("command.online.userlist", builder.toString());
    }

    public String getCommand() {
        return "/users";
    }

    public String getDescription() {
        return message("command.online.description");
    }

    public void process(User user, String data) {
        user.sendMessage(constructListOfAllUsers());
    }
}